package com.example.fitnessservice.service.impl;

import com.example.fitnessservice.module.dao.usermapper.BodyDataMapper;
import com.example.fitnessservice.module.pojo.UserBodyData;
import com.example.fitnessservice.module.utils.TimeUtils;
import com.example.fitnessservice.service.BodyDataService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Service
public class BodyServiceImpl implements BodyDataService {
    @Resource
    private BodyDataMapper bodyDataMapper;

    @Override
    public List<UserBodyData> findBodyDataById(Integer userId) {
        return bodyDataMapper.findBodyDataById(userId);
    }

    @Override
    public boolean addBodyDataById(UserBodyData userBodyData) throws ParseException {
        //bmi的计算方法：体重（kg）÷ 身高（m）^2
//        BigDecimal bmi = userBodyData.getUserWeight().divide(userBodyData.getUserHeight().multiply(userBodyData.getUserHeight()), BigDecimal.ROUND_CEILING);//保留两位小数
        double bmi = userBodyData.getUserWeight() / (userBodyData.getUserHeight() * userBodyData.getUserHeight());
        userBodyData.setBmi(bmi);
        Date submitTime = TimeUtils.getNowTime2();
        userBodyData.setSubmitTime(submitTime);

        int count = bodyDataMapper.addBodyData(userBodyData);
        return count == 1 ? true : false;
    }

    @Override
    public List<UserBodyData> findBodyDataByPage(Integer userId, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        return bodyDataMapper.findBodyDataByPage(userId);
    }
}
